﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel;
using Microsoft.Office.Word.Server.Conversions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WorkflowActions;
using System.ComponentModel;
using System.Workflow.ComponentModel.Compiler;

namespace SP2010WordAutomation.Workflow
{
    public class ConvertFolderActivity : Activity
    {
        public static DependencyProperty OutputFormatProperty = DependencyProperty.Register("OutputFormat", typeof(string), typeof(ConvertFolderActivity));
        public static DependencyProperty InputFolderUrlProperty = DependencyProperty.Register("InputFolderUrl", typeof(string), typeof(ConvertFolderActivity));
        public static DependencyProperty OutputFolderUrlProperty = DependencyProperty.Register("OutputFolderUrl", typeof(string), typeof(ConvertFolderActivity));
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(ConvertFolderActivity));
        public static DependencyProperty ConversionJobIdProperty = DependencyProperty.Register("ConversionJobId", typeof(string), typeof(ConvertFolderActivity));
        public static DependencyProperty SaveBehaviourProperty = DependencyProperty.Register("SaveBehaviour", typeof(string), typeof(ConvertFolderActivity));
        public static DependencyProperty UseRecursionProperty = DependencyProperty.Register("UseRecursion", typeof(Boolean), typeof(ConvertFolderActivity));
        
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string OutputFormat
        {
            get { return (string)base.GetValue(OutputFormatProperty); }
            set { base.SetValue(OutputFormatProperty, value); }
        }

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InputFolderUrl {
            get { return (string)base.GetValue(InputFolderUrlProperty); }
            set { base.SetValue(InputFolderUrlProperty, value); }
        }

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string OutputFolderUrl
        {
            get { return (string)base.GetValue(OutputFolderUrlProperty); }
            set { base.SetValue(OutputFolderUrlProperty, value); }
        }
                
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext)(base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ConversionJobId {
            get { return (string)base.GetValue(ConversionJobIdProperty); }
            set { base.SetValue(ConversionJobIdProperty, value); }
        }

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string SaveBehaviour
        {
            get { return (string)base.GetValue(SaveBehaviourProperty); }
            set { base.SetValue(SaveBehaviourProperty, value); }
        }

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Boolean UseRecursion
        {
            get { return (Boolean)base.GetValue(UseRecursionProperty); }
            set { base.SetValue(UseRecursionProperty, value); }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                Microsoft.Office.Word.Server.Service.WordServiceApplicationProxy wordServiceApplicationProxy = 
                    Common.GetWordServiceApplicationProxy(__Context.Site.WebApplication);
                ConversionJob job = new ConversionJob( wordServiceApplicationProxy );
                job.UserToken = __Context.InitiatorUser.UserToken;
                
                job.Settings.UpdateFields = true;
                job.Settings.OutputFormat = (SaveFormat)Enum.Parse(typeof(SaveFormat), OutputFormat);
                job.Settings.OutputSaveBehavior = (SaveBehavior)Enum.Parse(typeof(SaveBehavior), SaveBehaviour);                
                
                SPFolder inputfolder = __Context.Web.GetFolder(InputFolderUrl);
                SPFolder outputfolder = __Context.Web.GetFolder(OutputFolderUrl);
                                
                job.AddFolder(inputfolder, outputfolder, UseRecursion);
                job.Start();

                ConversionJobId = job.JobId.ToString();
                Common.AddCommentWorkflowHistory(string.Format("Conversion Job with Id={0} started", ConversionJobId), executionContext, WorkflowInstanceId);
            }
            catch (Exception ex)
            {
                Common.LogExceptionToWorkflowHistory(ex, executionContext, WorkflowInstanceId);
            }

            return ActivityExecutionStatus.Closed;
        }       
    }
}
